package com.smart.manager.member.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjectUtil;
import com.smart.common.entities.core.BaseSearch;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.domain.OrderAddress;
import com.smart.manager.member.entities.bodys.RMemberProfileAddress;
import com.smart.manager.member.entities.models.MemberProfileAddress;
import com.smart.manager.member.entities.models.query.QMemberProfileAddress;
import com.smart.wap.bodys.WapMemberBody;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商店组织库
 *
 * @author Administrator
 * @date 2023/04/02
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class MemberProfileAddressRepository {


    public Page<MemberProfileAddress> pageList(RMemberProfileAddress.SearchBody search) {
        QMemberProfileAddress query = new QMemberProfileAddress();
        Opt.ofBlankAble(search.getName()).ifPresent(x -> query.name.contains(x));
        query.order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<MemberProfileAddress>().load(query.findPagedList());
    }


    public void saveAddr(Long memberId, WapMemberBody.AddressBody body) {
        if (body.getDfFlag()) {
            cleanDefault(memberId);
        }
        String fullAddress = String.format("%s %s %s %s", body.getProvince(), body.getCity(), body.getDistrict(), body.getAddress());
        MemberProfileAddress memberAddress = BeanUtil.copyProperties(body, MemberProfileAddress.class);
        memberAddress.setMemberId(memberId);
        memberAddress.setFullAddress(fullAddress);
        memberAddress.save();
    }


    public void delMemberAddr(Long memberId, Long addressId) {
        MemberProfileAddress record = new QMemberProfileAddress().id.eq(addressId).setMaxRows(1).findOne();
        Assert.notNull(record, "删除失败 会员地址不存在或已被删除过了");
        Assert.equals(memberId, record.getMemberId(), "删除失败 非法的数据请求");
        record.delete();
    }

    public void updateMemberAddr(Long memberId, Long addressId, WapMemberBody.AddressBody body) {
        MemberProfileAddress record = new QMemberProfileAddress().id.eq(addressId).setMaxRows(1).findOne();
        Assert.notNull(record, "修改失败 会员地址不存在或已被删除过了");
        Assert.equals(memberId, record.getMemberId(), "修改失败 非法的数据请求");
        if (body.getDfFlag()) {
            cleanDefault(memberId);
        }
        String fullAddress = String.format("%s %s %s %s", body.getProvince(), body.getCity(), body.getDistrict(), body.getAddress());
        BeanUtil.copyProperties(body, record);
        record.setFullAddress(fullAddress).update();
    }


    public void setDefaultAddr(Long memberId, Long addressId) {
        MemberProfileAddress record = new QMemberProfileAddress().id.eq(addressId).setMaxRows(1).findOne();
        Assert.notNull(record, "修改失败 会员地址不存在或已被删除过了");
        Assert.equals(memberId, record.getMemberId(), "修改失败 非法的数据请求");
        cleanDefault(memberId);
        record.setDfFlag(true).update();
    }

    private void cleanDefault(Long memberId) {
        new QMemberProfileAddress().memberId.eq(memberId)
                .dfFlag.eq(true)
                .asUpdate()
                .set(QMemberProfileAddress.Alias.dfFlag, false)
                .update();
    }

    public Page<MemberProfileAddress> getPageList(Long memberId, BaseSearch search) {
        QMemberProfileAddress query = new QMemberProfileAddress();
        query.memberId.eq(memberId).order().dfFlag.desc().order().id.desc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<MemberProfileAddress>().load(query.findPagedList());
    }

    public OrderAddress readOrderAddress(Long memberId) {

        QMemberProfileAddress query = new QMemberProfileAddress();
        query.memberId.eq(memberId).setMaxRows(1).order().dfFlag.desc().order().id.desc();
        MemberProfileAddress rs = query.findOne();
        if (ObjectUtil.isNull(rs)) {
            return null;
        }

        return new OrderAddress().setName(rs.getName()).setMobile(rs.getMobile())
                .setCity(rs.getCity()).setProvince(rs.getProvince()).setDistrict(rs.getDistrict())
                .setDetail(rs.getAddress()).setFullAddress(rs.getFullAddress());


    }


    public List<MemberProfileAddress> getAllAddress(Long memberId) {
        QMemberProfileAddress query = new QMemberProfileAddress();
        query.memberId.eq(memberId).order().dfFlag.desc().order().id.desc();
        return query.findList();
    }
}
